package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class SpringRabbitListener {

    /**
     * 简单模型-消费者
     * @param msg
     */
    @RabbitListener(queues = "simple.queue")
    public void simpleListener(String msg){
        System.out.println("简单模型-消费者-消费到消息：" + msg);
    }

    @RabbitListener(queues = "work.queue")
    public void workListener1(String msg) throws InterruptedException {
        System.out.println("work模型-消费者1-消费到消息：" + msg);
        TimeUnit.MILLISECONDS.sleep(10);
    }

    @RabbitListener(queues = "work.queue")
    public void workListener2(String msg) throws InterruptedException {
        System.out.println("work模型-消费者2-消费到消息：" + msg);
        TimeUnit.MILLISECONDS.sleep(100);
    }

    /**
     * 广播模型-消费者1
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue1")
    public void fanoutListener1(String msg){
        System.out.println("广播模型-消费者1-消费到消息：" + msg);
    }

    /**
     * 广播模型-消费者2
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue2")
    public void fanoutListener2(String msg){
        System.out.println("广播模型-消费者2-消费到消息：" + msg);
    }

    /**
     * 路由模型-消费者1
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),
                                            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
                                            key = {"blue", "red"}))
    public void directListener1(String msg){
        System.out.println("路由模型-消费者1-消费到消息：" + msg);
    }

    /**
     * 路由模型-消费者2
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),
                                            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
                                            key = {"yellow", "red"}))
    public void directListener2(String msg){
        System.out.println("路由模型-消费者2-消费到消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue1"),
                                             exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
                                             key = {"china.#"}))
    public void topicListener1(String msg){
        System.out.println("主题模型-消费者1-消费到消息：" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = {"#.news"}))
    public void topicListener2(String msg) {
        System.out.println("主题模型-消费者2-消费到消息：" + msg);
    }
}